/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Marks;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Vector;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
/**
 *
 * @author Mukesh
 */
public class AddMarks extends HttpServlet {

    private Enumeration<String> htmlFields;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        try {
            Vector<MarksBean> list = new Vector<MarksBean>();
            htmlFields = request.getParameterNames();
            while (htmlFields.hasMoreElements()) {
                MarksBean mb = new MarksBean();
                String string = htmlFields.nextElement();
                if (string.contains("-")) {
                    String[] st = string.split("-");
                    mb.setAdmissionid(Integer.parseInt(st[0]));
                    mb.setExamtypeid(Integer.parseInt(st[1]));

                    if (request.getParameter(string).length() != 0) {
                        mb.setMarks(Integer.parseInt(request.getParameter(string)));
                    }
                    if (request.getParameter("subject").length() != 0) {
                        mb.setSubid(Integer.parseInt(request.getParameter("subject")));
                    }

                    mb.setUid(Integer.parseInt(session.getAttribute("uid").toString()));
                    if (request.getParameter("semester").length() != 0) {
                        mb.setSem(Integer.parseInt(request.getParameter("semester")));
                    }
                    
                    list.add(mb);
                }

            }
            int i = Marks.Query.addMarks(list);
            if (i != 0) {
                session.setAttribute("status", "Success");
                session.setAttribute("msg", i + " Record Updated.");
                response.sendRedirect("/RIT/Services/Examination/AddNumber.jsp");
            } else {
                session.setAttribute("status", "Fail");
                session.setAttribute("msg", "Marks Not Updated");
                response.sendRedirect("/RIT/Services/Examination/AddNumber.jsp");
            }

        } catch (Exception e) {
            session.setAttribute("status", "Fail");
            session.setAttribute("msg", "Marks Not Updated (e)");
            response.sendRedirect("/RIT/Services/Examination/AddNumber.jsp");
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
